<template>
  <div>
    <form-marker
      :isEdit="isEditPage" 
      :formData="list" 
      :rulesList="rules"
      :selectList="selectData"
      :eidtList="detailData" 
      v-on:on-confirm="onSaveInfo" 
      v-on:on-cancel="onCancelInfo"></form-marker>
  </div>
</template>
<script>
import FormMarker from '@/components/createPage/createMarker'
export default {
  components: {
    FormMarker
  },
  data () {
    // 自定义验证规则
    let checkCode = (rule, value, callback) => {
      let reg = /^\d{2}$/
      if (!reg.test(value) && this.pageType === 'new') {
        callback(new Error('编码仅支持2位数字，请重新输入'))
      } else if (value && !this.noRepeat && this.pageType === 'new') {
        this.$http.post(`/${this.$const.PREFIX.MAINDATA}/mproductclasses/check`, {itemNo: value}).then((res) => {
          if (res.result) {
            this.noRepeat = res.data // res.data.data [true]有重复/【false】无重复
            if (!res.data) {
              callback(new Error('编码已存在，请重新输入'))
            }
          }
        })
      } else {
        callback()
      }
    }
    return {
      list: [
        {
          type: 'input',
          label: '系列编码',
          name: '系列编码',
          prop: 'itemNo',
          isDisable: true
        },
        {
          type: 'input',
          label: '系列名称',
          name: '系列名称',
          prop: 'name'
        },
        {
          type: 'input',
          label: '系列英文名称',
          name: '系列英文名称',
          prop: 'enName'
        }
      ],
      rules: {
        itemNo: [
          { max: 32, message: '长度不可超过32个字符', trigger: 'change' },
          { required: true, message: '系列编码不允许为空', trigger: 'change' },
          { required: true, validator: checkCode, trigger: 'change' }
        ],
        name: [
          { max: 64, message: '长度不可超过64个字符', trigger: 'change' },
          { required: true, message: '系列名称不允许为空', trigger: 'blur' }
        ]
      },
      selectData: {},
      detailData: {},
      noRepeat: false,
      isEditPage: false // 判断是编辑or新增
    }
  },
  mounted () {
    this.pageType = this.$route.params.id
    this.getSeriesDetail()
  },
  methods: {
    // 获取编辑详情数据
    async getSeriesDetail () {
      if (this.pageType !== 'new') {
        let {data} = await this.$http.post(`/${this.$const.PREFIX.MAINDATA}/mproductclasses/getOne`, {id: this.pageType})
        this.detailData = data
        this.isEditPage = true
      }
    },
    // 接口保存新建信息
    onSaveInfo (val) {
      let url = this.pageType === 'new' ? `/${this.$const.PREFIX.MAINDATA}/mproductclasses/add` : `/${this.$const.PREFIX.MAINDATA}/mproductclasses/update`
      let params = val
      params.itemType = 1 // 1-系列 0-商品分类
      this.$http.post(url, params).then((res) => {
        if (res.result) {
          this.$message.success('操作成功')
          this.$router.push({name: 'series'})
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    onCancelInfo () {
      this.$router.push({name: 'series'})
    }
  }
}
</script>
<style lang="scss" scoped>

</style>


